unit Unit_Share;

interface

uses
  IniFiles, SysUtils, Winapi.Windows, Vcl.Dialogs, Winapi.MMSystem, Forms, DBGrids,
  Vcl.Graphics, synacode;

const
  LANG_ENG = '00000409';
  LANG_RUS = '00000422';

  check_error_count = 999999;

  mrArrears = 99;
  mode_Revenue = 97;

function DateTimeForSQL(const DateTime : TDateTime): String;
function DateForSQL(const Date : TDate): String;
function FloatForSQL(Input: Double): String;
function StrForSQL(Input: String): String;
function IniSET(ini_file: String; Sys: Boolean = False): TIniFile;
function StrToMD5(Input: String): String;

procedure RUS();
procedure ENG();
procedure FontToIni(FontDialog: TFontDialog);
procedure FontFromIni(Form: TForm);
procedure DBGridToIni(Form: TForm);
procedure DBGridFromIni(Form: TForm);


implementation

uses Komp_Unit1;


function StrToMD5(Input: String): String;
var
  hex, md5hash: String;
  ch: Byte;
  Bytes: TBytes;
begin
md5hash := MD5(Utf8ToAnsi(Input));
hex := '';
Bytes := BytesOf(md5hash);

  for ch in Bytes do
    hex := hex + IntToHex(ch, 2);

Result := hex;
end;

function IniSET(ini_file: String; Sys: Boolean = False): TIniFile;
var
  ini: TIniFile;
  path: String;
begin
path := Application.ExeName;
path := ExtractFileDir(path);

  if path[Length(path)] <> '\' then
    path := path + '\';

  if Sys = True then
    ini_file := path + ini_file
  else
    ini_file := path + user + '_' + ini_file;

ini := TIniFile.Create(ini_file);
Result := ini;
end;

procedure FontToIni(FontDialog: TFontDialog);
var
  ini: TIniFile;
begin
ini := IniSET('set.ini');

ini.WriteInteger('FONT', 'Charset', FontDialog.Font.Charset);
ini.WriteInteger('FONT', 'Color', FontDialog.Font.Color);
ini.WriteInteger('FONT', 'Height', FontDialog.Font.Height);
ini.WriteString('FONT', 'Name', FontDialog.Font.Name);
ini.WriteInteger('FONT', 'Orientation', FontDialog.Font.Orientation);
ini.WriteInteger('FONT', 'Pitch', Ord(FontDialog.Font.Pitch));
ini.WriteInteger('FONT', 'Size', FontDialog.Font.Size);
ini.WriteInteger('FONT', 'PixelsPerInch', FontDialog.Font.PixelsPerInch);

  if fsBold in FontDialog.Font.Style then
    ini.WriteBool('FONT', 'fsBold', True)
  else
    ini.WriteBool('FONT', 'fsBold', False);

  if fsItalic in FontDialog.Font.Style then
    ini.WriteBool('FONT', 'fsItalic', True)
  else
    ini.WriteBool('FONT', 'fsItalic', False);

  if fsUnderline in FontDialog.Font.Style then
    ini.WriteBool('FONT', 'fsUnderline', True)
  else
    ini.WriteBool('FONT', 'fsUnderline', False);

  if fsStrikeOut in FontDialog.Font.Style then
    ini.WriteBool('FONT', 'fsStrikeOut', True)
  else
    ini.WriteBool('FONT', 'fsStrikeOut', False);

ini.Free;
end;

procedure FontFromIni(Form: TForm);
var
  ini: TIniFile;
  FontDialog: TFontDialog;
  i: Integer;
begin
ini := IniSET('set.ini');
FontDialog := TFontDialog.Create(nil);

FontDialog.Font.Charset := ini.ReadInteger('FONT', 'Charset', 1);
FontDialog.Font.Color := ini.ReadInteger('FONT', 'Color', -16777208);
FontDialog.Font.Height := ini.ReadInteger('FONT', 'Height', -16);
FontDialog.Font.Name := ini.ReadString('FONT', 'Name', 'Tahoma');
FontDialog.Font.Orientation := ini.ReadInteger('FONT', 'Orientation', 0);
FontDialog.Font.Pitch := TFontPitch(ini.ReadInteger('FONT', 'Pitch', 0));
FontDialog.Font.Size := ini.ReadInteger('FONT', 'Size', 10);
FontDialog.Font.PixelsPerInch := ini.ReadInteger('FONT', 'PixelsPerInch', 96);

  if ini.ReadBool('FONT', 'fsBold', False) then
    FontDialog.Font.Style := FontDialog.Font.Style + [fsBold];

  if ini.ReadBool('FONT', 'fsItalic', False) then
    FontDialog.Font.Style := FontDialog.Font.Style + [fsItalic];

  if ini.ReadBool('FONT', 'fsUnderline', False) then
    FontDialog.Font.Style := FontDialog.Font.Style + [fsUnderline];

  if ini.ReadBool('FONT', 'fsStrikeOut', False) then
    FontDialog.Font.Style := FontDialog.Font.Style + [fsStrikeOut];

  for i := 0 to Form.ComponentCount - 1 do
      if Form.Components[i].ClassType = TDBGrid then
        (Form.Components[i] as TDBGrid).Font.Assign(FontDialog.Font);

FontDialog.Free;
ini.Free;
end;

procedure DBGridToIni(Form: TForm);
var
  ini: TIniFile;
  i, j: Integer;
begin
ini := IniSET('set.ini');

  for i := 0 to Form.ComponentCount - 1 do
      if Form.Components[i].ClassType = TDBGrid then
          for j := 0 to (Form.Components[i] as TDBGrid).Columns.Count - 1 do
          begin
            ini.WriteString((Form.Components[i] as TDBGrid).Name, 'FieldName_' + IntToStr(j), (Form.Components[i] as TDBGrid).Columns[j].FieldName);
            ini.WriteString((Form.Components[i] as TDBGrid).Name, 'Title_Caption_' + IntToStr(j), (Form.Components[i] as TDBGrid).Columns[j].Title.Caption);
            ini.WriteBool((Form.Components[i] as TDBGrid).Name, 'Visible_' + IntToStr(j), (Form.Components[i] as TDBGrid).Columns[j].Visible);
            ini.WriteInteger((Form.Components[i] as TDBGrid).Name, 'Width_' + IntToStr(j), (Form.Components[i] as TDBGrid).Columns[j].Width);

            ini.WriteInteger((Form.Components[i] as TDBGrid).Name, 'Color_' + IntToStr(j), (Form.Components[i] as TDBGrid).Columns[j].Color);
          end;

ini.Free;
end;

procedure DBGridFromIni(Form: TForm);
var
  ini: TIniFile;
  i, j: Integer;
begin
ini := IniSET('set.ini');

FontFromIni(Form);

  for i := 0 to Form.ComponentCount - 1 do
      if Form.Components[i].ClassType = TDBGrid then
          for j := 0 to (Form.Components[i] as TDBGrid).Columns.Count - 1 do
          begin
            (Form.Components[i] as TDBGrid).Columns[j].FieldName := ini.ReadString((Form.Components[i] as TDBGrid).Name, 'FieldName_' + IntToStr(j), (Form.Components[i] as TDBGrid).Columns[j].FieldName);
            (Form.Components[i] as TDBGrid).Columns[j].Title.Caption := ini.ReadString((Form.Components[i] as TDBGrid).Name, 'Title_Caption_' + IntToStr(j), (Form.Components[i] as TDBGrid).Columns[j].Title.Caption);
            (Form.Components[i] as TDBGrid).Columns[j].Visible := ini.ReadBool((Form.Components[i] as TDBGrid).Name, 'Visible_' + IntToStr(j), (Form.Components[i] as TDBGrid).Columns[j].Visible);
            (Form.Components[i] as TDBGrid).Columns[j].Width := ini.ReadInteger((Form.Components[i] as TDBGrid).Name, 'Width_' + IntToStr(j), (Form.Components[i] as TDBGrid).Columns[j].Width);

            (Form.Components[i] as TDBGrid).Columns[j].Color := ini.ReadInteger((Form.Components[i] as TDBGrid).Name, 'Color_' + IntToStr(j), (Form.Components[i] as TDBGrid).Columns[j].Color);

              if (Form.Components[i] as TDBGrid).Columns[j].Color = 0 then
                (Form.Components[i] as TDBGrid).Columns[j].Color := clWindow;
          end;

ini.Free;
end;

procedure RUS();
begin
  if GetKeyboardLayout(GetWindowThreadProcessId(GetForegroundWindow, nil)) = 67699721 then
    ActivateKeyboardLayout(LoadKeyboardLayout(LANG_RUS, KLF_ACTIVATE), 0);
end;

procedure ENG();
begin
  if GetKeyboardLayout(GetWindowThreadProcessId(GetForegroundWindow, nil)) <> 67699721 then
    ActivateKeyboardLayout(LoadKeyboardLayout(LANG_ENG, KLF_ACTIVATE), 0);
end;

function StrForSQL(Input: String): String;
begin
Result := '''' + StringReplace(Input, '''', '''''', [rfReplaceAll, rfIgnoreCase]) + '''';
end;

function FloatForSQL(Input: Double): String;
begin
Result := '''' + StringReplace(FloatToStr(Input), ',', '.', [rfReplaceAll, rfIgnoreCase]) + '''';
end;

function DateForSQL(const Date : TDate): String;
var
  y, m, d : word;
begin
  DecodeDate(Date, y, m, d) ;

  Result := Format('''%.*d-%.*d-%.*d''',[4, y, 2, m, 2, d]) ;
end;

function DateTimeForSQL(const DateTime : TDateTime): String;
var
  y, m, d : word;
  hour, min, sec, msec : word;
begin
  DecodeDate(DateTime, y, m, d) ;
  DecodeTime(DateTime, hour, min, sec, msec) ;

  Result := Format('''%.*d-%.*d-%.*d %.*d.%.*d.%.*d''',[4, y, 2, m, 2, d, 2, hour, 2, min, 2, sec]) ;
end;

end.
